import { defineStore } from 'pinia'
import { ref } from 'vue'

const initState = {
  token: '',
  avatar: '',
  nickName: '',
  openid: '',
  phonenumber: '',
  sex: '',
  userId: '',
}

export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref<IUserInfo>({ ...initState })
    const setUserInfo = (val: IUserInfo) => {
      userInfo.value = val
    }
    const clearUserInfo = () => {
      userInfo.value = { ...initState }
      uni.clearStorageSync()
    }
    const isLogined = computed(() => !!userInfo.value.token)
    return {
      userInfo,
      setUserInfo,
      clearUserInfo,
      isLogined,
    }
  },
  {
    persist: true,
  },
)
